package com.wencai.daily;

import com.wencai.daily.bean.FiveMinutesData;
import com.wencai.daily.utils.ReadUtils;
import com.wencai.util.ExchangeDay;
import com.wencai.util.IOUtils;

import java.io.IOException;
import java.util.*;

/**
 * 以五分钟涨速作为买卖依据
 *
 * @author wangpan
 * @date 2023/6/10
 */
public class FiveMinutesStrategy {
    private static String dir = "D:\\Users\\Administrator\\Desktop\\日线策略\\5分线数据2022";

    private static String five_filter_dir = "D:\\Users\\Administrator\\Desktop\\日线策略\\5分线数据2022_过滤\\";

    public static void main(String[] args) throws IOException {
        String exchangeDay = "20220101";
        while (true) {
            exchangeDay = ExchangeDay.getNextExchangeDay(exchangeDay);
            if (exchangeDay.compareTo("20221231") > 0) {
                break;
            }
            Map<String, List<FiveMinutesData>> fiveMinutesData = ReadUtils.readAllFiveMinutesData(dir, exchangeDay);
            Collection<List<FiveMinutesData>> values = fiveMinutesData.values();
            List<Double> list = filterValues(fiveMinutesData.values(), exchangeDay);
            System.out.println(exchangeDay + ":" + list.stream().mapToDouble(item -> item).average().getAsDouble());
        }

    }

    private static List<Double> filterValues(Collection<List<FiveMinutesData>> values, String exchangeDay) throws IOException {
        exchangeDay = ExchangeDay.getNextExchangeDay(exchangeDay);
        if (exchangeDay.compareTo("20221231") > 0) {
            return new ArrayList<>();
        }
        List<Double> d = new ArrayList<>();

        for (List<FiveMinutesData> fiveMinutesDataList : values) {
            if (fiveMinutesDataList.isEmpty()) {
                continue;
            }
            FiveMinutesData fiveMinutesData = fiveMinutesDataList.get(0);
            double open = fiveMinutesData.getOpen();
            double close = fiveMinutesData.getClose();
            if (close / open < 1.05) {
                continue;
            }
            double buyPrice = fiveMinutesDataList.get(1).getOpen();
            List<FiveMinutesData> fiveMinutesData1 = ReadUtils.readFiveMinutesData(dir, fiveMinutesData.getCode() + ".txt", exchangeDay);
            if (fiveMinutesData1.isEmpty()) {
                continue;
            }
            FiveMinutesData fiveMinutesData2 = fiveMinutesData1.get(1);
            double sellPrice = fiveMinutesData2.getOpen();
            double gainRate = sellPrice / buyPrice;
            d.add(gainRate);

            StringBuilder builder = new StringBuilder();
            merge(builder, fiveMinutesDataList, fiveMinutesData1);
            IOUtils.writeFile(five_filter_dir + fiveMinutesData.getDate() + "_" + fiveMinutesData.getCode() + ".txt", builder.toString());
        }
        return d;
    }

    private static void merge(StringBuilder builder, List<FiveMinutesData> fiveMinutesDataList, List<FiveMinutesData> fiveMinutesData1) {
        for (FiveMinutesData fiveMinutesData : fiveMinutesDataList) {
            builder.append(convertToString(fiveMinutesData)).append(System.lineSeparator());
        }
        for (FiveMinutesData fiveMinutesData : fiveMinutesData1) {
            builder.append(convertToString(fiveMinutesData)).append(System.lineSeparator());
        }
    }

    private static String convertToString(FiveMinutesData fiveMinutesData) {
        StringJoiner joiner = new StringJoiner(",");
        joiner.add(fiveMinutesData.getDate())
                .add(fiveMinutesData.getTime())
                .add(fiveMinutesData.getCode())
                .add(String.valueOf(fiveMinutesData.getOpen()))
                .add(String.valueOf(fiveMinutesData.getHigh()))
                .add(String.valueOf(fiveMinutesData.getLow()))
                .add(String.valueOf(fiveMinutesData.getClose()))
                .add(String.valueOf((long) fiveMinutesData.getVolume()))
                .add(String.valueOf((long) fiveMinutesData.getAmount()))
                .add(String.valueOf(fiveMinutesData.getAdjustFlag()));
        return joiner.toString();
    }
}
